package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.constant.UserConstant;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WeMediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname>
        implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private WeMediaFeign weMediaFeign;

    @Autowired
    private ArticleFeign articleFeign;


    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        // 调用mybatisplus的api进行列表查询
        // 1.参数校验
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.分页参数的校验
        dto.checkParam();

        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (null != dto.getStatus()) {
            lambdaQueryWrapper.like(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage<ApUserRealname> apUserRealnameIPage = page(page, lambdaQueryWrapper);

        PageResponseResult pageResponseResult =
                new PageResponseResult(dto.getPage(), dto.getSize(), (int) apUserRealnameIPage.getTotal());
        pageResponseResult.setData(apUserRealnameIPage.getRecords());
        return pageResponseResult;
    }

    @Override
    @GlobalTransactional
    public ResponseResult authRealName(AuthDto dto, short status) {
        // 1.参数校验
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 2.正常更新
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(status);
        if (StringUtils.isNotBlank(dto.getMsg())) {
            apUserRealname.setReason(dto.getMsg());
        }
        boolean updateResult = updateById(apUserRealname);
        if (!updateResult) {
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL, "审核失败");
        }
        // 3.做判断
        if (status == UserConstant.AUTH_PASS) {
            // 生成自媒体账号
            ApUserRealname userRealname = this.getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
            // 通过feign查询当前的app用户是否已经有自媒体账号，如果没有就进行创建
            WmUser wmUser = weMediaFeign.queryWmUserByName(apUser.getName());
            if (wmUser == null) {
                // 调用feign接口来新建自媒体账号
                wmUser = new WmUser();
                wmUser.setApUserId(apUser.getId());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setPhone(apUser.getPhone());
                wmUser.setStatus(9);
                wmUser.setCreatedTime(new Date());
                weMediaFeign.saveWmUser(wmUser);
            }
            // 更新一下apUser的flag为1
            apUser.setFlag((short)1);
            apUserMapper.updateById(apUser);
            // 文章作者账号
            ApAuthor apAuthor = articleFeign.findByUserId(apUser.getId());
            if (null == apAuthor) {
                apAuthor = new ApAuthor();
                apAuthor.setName(apUser.getName());
                apAuthor.setType(2);
                apAuthor.setUserId(apUser.getId());
                apAuthor.setCreatedTime(new Date());
                articleFeign.save(apAuthor);
            }
        }
        int i = 6/0;
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
